package assetsmanagement

import (
	"k8s-admin/service/assetsmanagement"
	"k8s-admin/utils"

	"github.com/gin-gonic/gin"
)

func AssetsManagementCreate(ctx *gin.Context) {
	params := new(struct {
		AssetsName  string `json:"assets_name" binding:"required"`
		Port        int    `json:"port" binding:"required"`
		Username    string `json:"username" binding:"required"`
		Password    string `json:"password" binding:"required"`
		Host        string `json:"host" binding:"required"`
		Status      *int   `json:"status" binding:"required"`
		AssetsClass string `json:"assets_class" binding:"required"`
	})
	if err := ctx.ShouldBindJSON(&params); err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())
	} else {
		err := assetsmanagement.NewAssetsManagementService().Create(params.AssetsClass, params.Username, params.Password, params.AssetsName, params.AssetsClass, &params.Port, params.Status)
		if err != nil {
			utils.ReturnContext(ctx).Failed("失败", err.Error())
		} else {
			utils.ReturnContext(ctx).Successful("成功", "添加资源数据，成功")
		}
	}
}

/*设备是否在线检测接口*/
func HeartBeat(ctx *gin.Context) {
	params := new(struct {
		HostIP string `form:"host_ip" binding:"required"`
	})
	if err := ctx.ShouldBind(&params); err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())
	} else {
		ok := utils.Check_Ping(params.HostIP)
		if ok {
			utils.ReturnContext(ctx).Successful("成功", "在线")
		} else {
			utils.ReturnContext(ctx).Failed("失败", "不在线")
		}
	}
}

func AssetsManagementList(ctx *gin.Context) {
	params := new(struct {
		Limit int `form:"limit" binding:"required"`
		Page  int `form:"page" binding:"required"`
	})
	if err := ctx.ShouldBind(&params); err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())
	} else {
		list, err := assetsmanagement.NewAssetsManagementService().List(params.Limit, params.Page)
		if err != nil {
			utils.ReturnContext(ctx).Failed("失败", err.Error())
		} else {
			utils.ReturnContext(ctx).Successful("成功", list)
		}
	}
}

func AssetsManagementDelete(ctx *gin.Context) {
	params := new(struct {
		AssetsUID string `form:"assets_uuid" binding:"required"`
	})
	if err := ctx.ShouldBind(&params); err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())
	} else {
		err := assetsmanagement.NewAssetsManagementService().Delete(params.AssetsUID)
		if err != nil {
			utils.ReturnContext(ctx).Failed("失败", err.Error())
		} else {
			utils.ReturnContext(ctx).Successful("成功", "删除资源数据，成功")
		}
	}
}

func AssetsManagementUpdate(ctx *gin.Context) {
	params := new(struct {
		AssetsUID   string `json:"assets_uuid" binding:"required"`
		AssetsName  string `json:"assets_name" binding:"required"`
		Port        *int   `json:"port" binding:"required"`
		Username    string `json:"username" binding:"required"`
		Password    string `json:"password" binding:"required"`
		Host        string `json:"host" binding:"required"`
		Status      *int   `json:"status" binding:"required"`
		AssetsClass string `json:"assets_class" binding:"required"`
	})
	if err := ctx.ShouldBindJSON(&params); err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())
	} else {
		err := assetsmanagement.NewAssetsManagementService().Update(params.AssetsUID, params.AssetsClass, params.Username, params.Password, params.AssetsName, params.AssetsClass, params.Port, params.Status)
		if err != nil {
			utils.ReturnContext(ctx).Failed("失败", err.Error())

		} else {
			utils.ReturnContext(ctx).Successful("成功", "更新资源数据，成功")
		}
	}
}

func GetLocalHostCpuInfo(ctx *gin.Context) {
	cpuInfo, err := assetsmanagement.NewAssetsManagementService().GetLocalHostCpuInfo()
	if err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())
	} else {
		utils.ReturnContext(ctx).Successful("成功", cpuInfo)
	}
}

func GETLocalHostCpuUsageRate(ctx *gin.Context) {
	cpuUsageRate, err := assetsmanagement.NewAssetsManagementService().GETLocalHostCpuUsageRate()
	if err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())
	} else {
		utils.ReturnContext(ctx).Successful("成功", cpuUsageRate)
	}
}

// CPU使用率等信息展示接口
func GETLocalHostCpuUsageRateDetail(ctx *gin.Context) {
	cpuUsageRateDetail, err := assetsmanagement.NewAssetsManagementService().GETLocalHostCpuUsageRateDetail()
	if err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())
	} else {
		utils.ReturnContext(ctx).Successful("成功", cpuUsageRateDetail)
	}
}

// 用于资源监控的接口，获取CPU使用率等信息
func GETCPUUsageRates(ctx *gin.Context) {
	params := new(struct {
		AssetsUID string `form:"assets_uuid" binding:"required"`
	})
	if err := ctx.ShouldBind(&params); err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())
	} else {
		list, err := assetsmanagement.NewAssetsManagementService().GETHOSTCPUInfo(params.AssetsUID)
		if err != nil {
			utils.ReturnContext(ctx).Failed("失败", err.Error())
		} else {
			utils.ReturnContext(ctx).Successful("成功", list)
		}
	}

}

// 统一资源展示接口

func AssetsManagementInfoAll(ctx *gin.Context) {
	list, err := assetsmanagement.NewAssetsManagementService().AssetsManagementInfoAll()
	if err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())
	} else {
		utils.ReturnContext(ctx).Successful("成功", list)
	}
}
